# a small sample of special functions
#  David Morton 2008
from numpy import exp, sqrt, pi, cos, sin

def gaussian(center,peak,width,x):
    # simple normal distribution function with center offset
    return peak * exp(-(x-center)**2/width**2)

def gaussian2Dgeneral( center, peak, a, b, c, pos):
    # a general form of the 2D gaussian function.
    exponent = a*(pos[0]-center[0])**2 + \
               2*b*(pos[0]-center[0])*(pos[1]-center[1]) + \
               c*(pos[1]-center[1])**2
    return peak*exp(-exponent)

def gaussian2D( center, peak, widths, angle, pos):
    # extention of gaussian function into two dimensions.
    # convert widths and angle into a,b,c for general form.
    # it should be noted that these widths are half-widths.
    angle = -angle # so that angle is counter clockwise looking down neg-z axis.
    a = cos(angle)**2/(2*widths[0]**2) + sin(angle)**2/(2*widths[1]**2)
    b = -sin(2*angle)/(4*widths[0]**2) + sin(2*angle)/(4*widths[1]**2)
    c = sin(angle)**2/(2*widths[0]**2) + cos(angle)**2/(2*widths[1]**2)
    return gaussian2Dgeneral(center, peak, a, b, c, pos)

def normal( mu, std, x ):
    # the normal distribution
    peak = (   1/(std*sqrt(2*pi))  ) 
    width = sqrt(2)*std
    return gaussian( mu, peak, width, x )

def exp_decay(scale,xoffset,yoffset,tau,x):
    # exponential decay function
    return scale * exp(-(x-xoffset)/tau) + yoffset

def alpha(onset,tau,gmax,t):
    """ returns:
            gmax * (t-onset)/tau * exp(-(t - onset - tau)/tau)
        alpha function is zero before onset.
        alpha function reaches maximum (gmax) at t = tau + offset. 
        units:
            returns --> units(gmax) 
            t,tau,onset --> time (must be the same units) """
    if t <= onset:
        return 0
    else:
        return gmax * (t-onset)/tau * exp(-(t - onset - tau)/tau)
 

